package com.example.sw.myapp1005;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.sw.myapp1005.Bean.AlamItem;

import java.util.List;

public class NaoZhongAdapter2 extends ArrayAdapter<AlamItem> {

    private int rid;

    public NaoZhongAdapter2(@NonNull Context context, int resource, @NonNull List<AlamItem> objects) {
        super(context, resource, objects);
        rid = resource;

    }

    @Override
    public View getView(int i, View view, ViewGroup parent) {

        //指定布局
        View v = LayoutInflater.from(getContext()).inflate(rid, null);
        //控件实例化
        TextView txtTime = v.findViewById(R.id.txtTime);
        Switch swbtn = v.findViewById(R.id.swbtn);

        final LinearLayout itemLayout=v.findViewById(R.id.itemLayout);

        //数据填充布局
        final AlamItem a = getItem(i);
        txtTime.setText(a.getTime());
        swbtn.setChecked(a.isIschecked());
        int c=a.isIschecked()? Color.GRAY:Color.WHITE;
        itemLayout.setBackgroundColor(c);

        //设置监听器
        swbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                a.setIschecked(isChecked);
                int c =isChecked ? Color.GRAY:Color.WHITE;
                //itemLayout设置背景色
                itemLayout.setBackgroundColor(c);
            }
        });



        //返回V
        return v;


    }
}